home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / aspipat.zip / CTCTRL.C < prev    next >
C/C++ Source or Header  |  1992-01-26  |  7KB  |  262 lines

  1. #ident "$Id: ctctrl.c,v 1.1 1992/01/15 01:15:30 chris Exp $"
  2.  
  3. /*
  4.  *
  5.  * Rev. 1.0  Very first revision for ix386 - 1989/12/18
  6.  * $Log: ctctrl.c,v $
  7.  * Revision 1.1  1992/01/15  01:15:30  chris
  8.  * Initial revision
  9.  *
  10.  *
  11.  *
  12.  * chris@alderan.sdata.de
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <time.h>
  17. #include <string.h>
  18. #include <errno.h>
  19.  
  20.  
  21. #define DWORD unsigned long
  22.  
  23. #include "aspi.h"
  24. #include "scsi.h"
  25. #include "scsierr.h"
  26.  
  27. extern int adapter_id;
  28. extern int scsi_tape_id;
  29. extern int lun_id;
  30. #define TARGET adapter_id, scsi_tape_id, lun_id
  31.  
  32.  
  33. extern char *getenv();
  34. extern void aspiinit();
  35. extern char *optarg;
  36.  
  37.  
  38. static char *ctctrl_id = "$Id: ctctrl.c,v 1.1 1992/01/15 01:15:30 chris Exp $";
  39.  
  40. char *
  41. index (s, c)
  42.     char *s;
  43.     int c;
  44. {
  45.     return (strchr (s, c));
  46. }
  47.  
  48. void
  49. bcopy (s1, s2, n)
  50.     char *s1, *s2;
  51.     int n;
  52. {
  53.     (void) memcpy (s2, s1, n);
  54. }
  55.  
  56.  
  57. void usage(cmd)
  58. char *cmd;
  59. {
  60.     fprintf(stderr,"%s: tape control for SCSI stramer using ASPI\n",cmd);
  61.     fprintf(stderr,"usage: %s [options]\n",cmd);
  62.     fprintf(stderr,"Options:   -e     -  erase entire tape\n");
  63.     fprintf(stderr,"           -t     -  retention tape\n");
  64.     fprintf(stderr,"           -r     -  reset tape device\n");
  65.     fprintf(stderr,"           -w     -  rewind tape (DEFAULT)\n");
  66.     fprintf(stderr,"           -p [n] -  position tape to file [n]\n");
  67.     fprintf(stderr,"                     n = 0 , position to logical end-of-tape (LEOT)\n");
  68.     fprintf(stderr,"                     n < 0 , position relative to LEOT\n");
  69.     fprintf(stderr,"           -q     -  quiet mode  -- don't ask questions\n");
  70. }
  71.  
  72.  
  73. int main(argc,argv)
  74. int argc;
  75. char **argv;
  76. {
  77.     char c, *cmd, *dev;
  78.     int  eflag,tflag,rflag,wflag,pflag,qflag,pos;
  79.  
  80.     cmd = argv[0];
  81.     eflag = tflag = rflag = wflag = pflag = qflag = pos = 0;
  82.  
  83.     while ( (c=getopt(argc,argv,"etrwp:?q")) != -1 )
  84.     switch ( c )  {
  85.         case 'e':
  86.         eflag = 1;
  87.         break;
  88.         case 't':
  89.         tflag = 1;
  90.         break;
  91.         case 'r':
  92.         rflag = 1;
  93.         break;
  94.         case 'w':
  95.         wflag = 1;
  96.         break;
  97.         case 'q':
  98.         qflag = 1;
  99.         break;
  100.         case 'p':
  101.         pflag = 1;
  102.         if ( optarg )  pos = atoi(optarg);
  103.         break;
  104.         case '?':
  105.         usage(cmd);
  106.                 exit(-1);
  107.     }
  108.  
  109.     if ( ! (eflag||tflag||rflag||wflag||pflag) ) wflag = 1;  /* default ! */
  110.  
  111.     if ( rflag )  eflag = tflag = wflag = pflag = 0;
  112.  
  113.     /* Our aspi - open() */
  114.     aspiinit();
  115.  
  116.     if ( eflag )  {
  117.     if ( ! qflag )  {
  118.             printf("ARE YOU SURE YOU WANT TO ERASE THE TAPE (Y/N) ? ");
  119.         fflush(stdout);
  120.         c = getchar();
  121.         if ( (c!='y') && (c!='Y') )  {
  122.         fprintf(stderr,"%s: operation canceled\n",cmd);
  123.         exit(0);
  124.         }
  125.     }
  126.     pflag = 0;
  127.     }
  128.  
  129.     if ( rflag )  {
  130.       clock_t clk;
  131.       int i;
  132.  
  133.       if ( SCSIResetDevice(TARGET) )  {
  134.         ( void ) fprintf(stderr,"Error resetting Tape\n");
  135.         exit(-1);
  136.       }
  137.       clk = clock();
  138.       clk += ( clock_t ) 3 * CLK_TCK;
  139.       while ( clock() < clk ) ;       /* delay three seconds */
  140.  
  141.       while ( (i=SCSIRewind(TARGET, 0, 0)) == E$UnitAttention )  ;
  142.       if ( i )  {
  143.         ( void ) fprintf(stderr,"Error rewinding Tape\n");
  144.         exit(-1);
  145.       }
  146.  
  147.     }
  148.  
  149.     if ( tflag )  {  /* retension tape */ 
  150.         if ( SCSIRewind(TARGET, 0, 0) )  {
  151.             ( void ) fprintf(stderr,"Error rewinding Tape\n");
  152.             exit(-1);
  153.     }
  154.         if ( ! qflag ) printf("retention tape .. ");
  155.     fflush(stdout);
  156.         if ( SCSILoad(TARGET, 0, 1, 1, 0, 0) )  {
  157.             ( void ) fprintf(stderr,"Error retention Tape\n");
  158.             ( void ) SCSIRewind(TARGET, 0, 0 );
  159.             exit(-1);
  160.  
  161.         }
  162.  
  163.         if ( SCSILoad(TARGET, 0, 0, 0, 0, 0) )  {
  164.             ( void ) fprintf(stderr,"Error unloading Tape\n");
  165.             exit(-1);
  166.     }
  167.  
  168.     }
  169.  
  170.     if ( wflag )  {
  171.     if ( ! qflag ) printf("rewinding tape .. ");
  172.     fflush(stdout);
  173.         if ( SCSIRewind(TARGET, 0, 0) ) {
  174.             ( void ) fprintf(stderr,"Error rewinding Tape\n");
  175.             exit(-1);
  176.     }
  177.     if ( ! qflag ) printf("[OK]\n");
  178.     }
  179.  
  180.     if ( eflag )  {
  181.         if ( SCSILoad(TARGET, 0, 1, 0, 0, 0) )  {
  182.             ( void ) fprintf(stderr,"Error loading Tape\n");
  183.             ( void ) SCSIRewind(TARGET, 0, 0 );
  184.             exit(-1);
  185.     }
  186.     if ( ! qflag )  printf("eraseing tape .. ");
  187.     fflush(stdout);
  188.  
  189.         if ( SCSIErase(TARGET, 0, 1, 0) )  {
  190.             ( void ) fprintf(stderr,"Error eraseing Tape\n");
  191.             ( void ) SCSIRewind(TARGET, 0, 0 );
  192.             exit(-1);
  193.     }
  194.  
  195.     if ( ! qflag ) printf("[OK]\n");
  196.  
  197.         if ( SCSILoad(TARGET, 0, 0, 0, 0, 0) ) {
  198.             ( void ) fprintf(stderr,"Error unloading Tape\n");
  199.             exit(-1);
  200.     }
  201.  
  202.     }
  203.  
  204.     if ( pflag )  {
  205.         if ( SCSIRewind(TARGET, 0, 0) ) {
  206.             ( void ) fprintf(stderr,"Error rewinding Tape\n");
  207.             exit(-1);
  208.     }
  209.  
  210.     if ( pos > 0 )  {
  211.         if ( ! qflag ) printf("seeking to tapefile #%d .. ",pos);
  212.             fflush(stdout);
  213.             if ( pos > 1 )  {
  214.               if ( SCSISpace(TARGET, 1, ( long ) pos - 1l, 0) )   {
  215.                 ( void ) fprintf(stderr,"Error positioning Tape\n");
  216.                 exit(-1);
  217.               }
  218.             }
  219.         if ( ! qflag ) printf("[OK]\n");
  220.     } else  if ( pos == 0 ) {
  221.         if ( ! qflag ) printf("seeking to End of Tape ");
  222.         fflush(stdout);
  223.             pos = 1;
  224.             while ( !SCSISpace(TARGET, 1, 1l, 0) ) {
  225.               ++pos;
  226.               if ( ! qflag ) putchar('.');
  227.               fflush(stdout);
  228.         }
  229.         if ( ! qflag ) printf(" next tapefile is #%d [OK]\n",pos);
  230.     } else if ( pos < 0 )  {
  231.         int p;
  232.  
  233.         if ( ! qflag ) printf("seeking to file EOT%d ",pos);
  234.         fflush(stdout);
  235.  
  236.             p = 1;
  237.             while ( !SCSISpace(TARGET, 1, 1l, 0) )  {
  238.                 ++p;
  239.         if ( ! qflag ) putchar('.');
  240.         fflush(stdout);
  241.         }
  242.             if ( SCSIRewind(TARGET, 0, 0) ) {
  243.               ( void ) fprintf(stderr,"Error rewinding Tape\n");
  244.               exit(-1);
  245.             }
  246.         p += pos;
  247.         if ( p < 1 ) p = 1;
  248.         if ( ! qflag ) printf(" tapefile #%d .. ",p);
  249.         fflush(stdout);
  250.             if ( p > 1 )  {
  251.               if ( SCSISpace(TARGET, 1, ( long ) p - 1l, 0) )   {
  252.                 ( void ) fprintf(stderr,"Error positioning Tape\n");
  253.                 exit(-1);
  254.               }
  255.             }
  256.  
  257.         if ( ! qflag ) printf("[OK]\n");
  258.     }
  259.     }
  260.     return pflag ? pos-1 : 0;
  261. }
  262.